package com.raspositos.guitarfrik.android;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ImageArrayAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Activity mContext;

    private TrackDescriptor[] tracks;
	private int width;
	private int height;
	private ImageView.ScaleType scale;
//	private String[] title;

    public ImageArrayAdapter(Activity c, TrackDescriptor [] tracks,  int width, int height, ImageView.ScaleType scale, int background) {
        mContext = c;
        mGalleryItemBackground = background;
        this.width = width;
        this.height = height;
        this.tracks = tracks;
        this.scale = scale;
//        this.title = title;
    }

    public synchronized void setTracks( TrackDescriptor [] tracks){
    	this.tracks = tracks;
    }
    public int getCount() {
        return tracks.length;
    }

    public Object getItem(int position) {
        return tracks[position];
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    	View v = mContext.getLayoutInflater().inflate(R.layout.gallery_item, null);
        ImageView i = (ImageView) v.findViewById(R.id.image);
        TrackDescriptor track = tracks[position];
		i.setImageDrawable(track.getAlbumArt(mContext));
        i.setLayoutParams(new RelativeLayout.LayoutParams(width, height));
        i.setScaleType(scale);
        i.setBackgroundResource(mGalleryItemBackground);
        TextView t = (TextView) v.findViewById(R.id.txtview);
        t.setText(track.getArtist() + " - " + track.getAlbum() + " - " + track.getTitle());
        v.setTag(track);
        //v.setLayoutParams(new Gallery.LayoutParams(width, height));
        t.setWidth(width);
        return v;
    }
    
   public int findPosition(TrackDescriptor track){
	   int pos = 0;
	   for (int i = 0; i < tracks.length; i++) {
		   if (tracks[i].equals(track)){
			   pos = i;
			   break;
		   }
	   }
	   return pos;
   }
   
}
